Component({
  properties: {
    visible: {
      type: Boolean,
      value: false
    },
    activeReservations: {
      type: Array,
      value: []
    },
    historyReservations: {
      type: Array,
      value: []
    }
  },

  data: {
    year: 0,
    month: 0,
    days: []
  },

  observers: {
    'visible': function(visible) {
      if (visible) {
        this.initCalendar();
      }
    },
    'activeReservations, historyReservations': function() {
      if (this.data.visible) {
        this.generateCalendar();
      }
    }
  },

  methods: {
    initCalendar() {
      const now = new Date();
      this.setData({
        year: now.getFullYear(),
        month: now.getMonth() + 1
      });
      this.generateCalendar();
    },

    generateCalendar() {
      const { year, month } = this.data;
      const firstDay = new Date(year, month - 1, 1);
      const lastDay = new Date(year, month, 0);
      const daysInMonth = lastDay.getDate();
      
      // 获取第一天是星期几（0=周日，1=周一...）
      let firstDayOfWeek = firstDay.getDay();
      // 转换为周一开始（0=周一，1=周二...）
      firstDayOfWeek = firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1;
      
      const days = [];
      
      // 添加上个月的日期
      const prevMonth = month === 1 ? 12 : month - 1;
      const prevYear = month === 1 ? year - 1 : year;
      const prevMonthLastDay = new Date(prevYear, prevMonth, 0).getDate();
      
      for (let i = firstDayOfWeek - 1; i >= 0; i--) {
        days.push({
          day: prevMonthLastDay - i,
          date: `${prevYear}-${String(prevMonth).padStart(2, '0')}-${String(prevMonthLastDay - i).padStart(2, '0')}`,
          type: 'other-month',
          hasReservation: false
        });
      }
      
      // 添加当前月的日期
      for (let day = 1; day <= daysInMonth; day++) {
        const dateStr = `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
        const reservationInfo = this.checkReservation(dateStr);
        
        days.push({
          day,
          date: dateStr,
          type: 'current-month',
          hasReservation: reservationInfo.hasReservation,
          isActiveReservation: reservationInfo.isActive
        });
      }
      
      // 添加下个月的日期，补齐42个格子（6周）
      const nextMonth = month === 12 ? 1 : month + 1;
      const nextYear = month === 12 ? year + 1 : year;
      const remainingDays = 42 - days.length;
      
      for (let day = 1; day <= remainingDays; day++) {
        days.push({
          day,
          date: `${nextYear}-${String(nextMonth).padStart(2, '0')}-${String(day).padStart(2, '0')}`,
          type: 'other-month',
          hasReservation: false
        });
      }
      
      this.setData({ days });
    },

    checkReservation(dateStr) {
      const { activeReservations, historyReservations } = this.data;
      
      // 检查是否是有效预约 - 使用formattedDate字段
      const isActiveReservation = activeReservations.some(reservation => {
        return reservation.formattedDate === dateStr;
      });
      
      // 检查是否是历史预约
      const isHistoryReservation = historyReservations.some(reservation => {
        return reservation.formattedDate === dateStr || reservation.date === dateStr || reservation.reservation_date === dateStr;
      });
      
      return {
        hasReservation: isActiveReservation || isHistoryReservation,
        isActive: isActiveReservation
      };
    },

    prevMonth() {
      let { year, month } = this.data;
      month--;
      if (month < 1) {
        month = 12;
        year--;
      }
      this.setData({ year, month });
      this.generateCalendar();
    },

    nextMonth() {
      let { year, month } = this.data;
      month++;
      if (month > 12) {
        month = 1;
        year++;
      }
      this.setData({ year, month });
      this.generateCalendar();
    },

    closePopup() {
      this.triggerEvent('close');
    }
  }
});